﻿package controller;

import bean.*;
import biz.UserBiz;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    public UserBiz ub;

    @RequestMapping("/TrainingItemBank")
    @ResponseBody
    public String findTrainingItemBank(int count,Integer courseId,String answer,String callback,HttpServletRequest request){  //鍙栧嚭棰樺簱琛ㄤ腑鐨勯鐩拰绛旀
        String[] trueAnswer=new String[20];  //鏀惧叆鐨勬槸 鐢ㄦ埛鐨勭瓟妗�
        HashMap map=new HashMap();//閫佺粰鍓嶇鐨勬暟鎹泦鍚�
        AnswerCardInfo answerCardInfo =new AnswerCardInfo();
        int i=0;
        String result="";
        CourseInfo courseInfo=ub.findAllCourseId(courseId);
         Stack<TrainingItemBankInfo> trainingItemBankInfo1=ub.findAllTrainingItemBankInfo(courseInfo.getExamId()); //得到所有的试题信息	
        Stack<TrainingItemBankInfo> trainingItemBankInfo2= new Stack<>();
        while(!trainingItemBankInfo1.empty()){   // 浠庢暟鎹簱涓緱鍒扮殑鍏冪礌锛屾爤1涓嶇┖锛岀幇灏嗘爤1鎵�鏈夌殑鍏冪礌瀛樺叆鏍�2 涓�
            trainingItemBankInfo2.push(trainingItemBankInfo1.peek());//鏍�1涓殑鏁版嵁瀛樺叆鏍�2涓�
            trainingItemBankInfo1.pop();
        }
        //鐜板湪鏍� 1涓虹┖锛屽垯鏍�2姣忓嚭涓�涓厓绱狅紝灏卞叆鏍�2  涓��
        if(count==1){  //  1  杩欐槸绗竴棰�
            map.put(trainingItemBankInfo2.peek(),null);//閫佺粰鍓嶇鐨勬暟鎹� 鏀惧叆寰幆涓�   //鏍�1 鐨勫厓绱犲嚭鏍�
            trainingItemBankInfo1.push(trainingItemBankInfo2.peek());//鏍�1 瀛樺叆 鏍�2 鍑虹殑鍏冪礌
            trainingItemBankInfo2.pop();//鏍�2 鍑烘爤
        }
        if(count==2){  //  2  琛ㄧず涓嬩竴棰�
            trueAnswer[i]=answer;
            i++;
            map.put(trainingItemBankInfo2.peek(),null);//閫佺粰鍓嶇鐨勬暟鎹�
            trainingItemBankInfo1.push(trainingItemBankInfo2.peek());//鏍�1 瀛樺叆 鏍�2 鍑虹殑鍏冪礌
            trainingItemBankInfo2.pop();//鏍�2 鍑烘爤
        }
        if (count==3||!trainingItemBankInfo1.empty()){ // 3  琛ㄧず涓婁竴棰�
            trueAnswer[i]=answer;
            map.put(trainingItemBankInfo1.peek(),trueAnswer[i]);//鍔犲叆棰樼洰鍜岀敤鎴风殑绛旀
            //System.out.println(map.keySet()+" "+map.get(trainingItemBankInfo1.peek()));//鏍�1 鐨勫厓绱犲嚭鏍�
            i--;
            trainingItemBankInfo2.push(trainingItemBankInfo1.peek()); //鏍�1 鍑虹殑鍏冪礌 鍏ユ爤2 涓�
            trainingItemBankInfo1.pop();//鏍�1 鍑烘爤
        }
        if(count==4){ //   4  琛ㄧず 鏈�鍚庝竴棰樼殑绛旀
            trueAnswer[i]=answer;
            int num=5;
            int index=0;
            int j=0;
            //寮�濮嬪線鏁版嵁搴撲腑鎻掑叆鎵�鏈夌殑绛旀
            answerCardInfo.setUsersid(1);  //寰楀埌鐢ㄦ埛鐨刬d
            answerCardInfo.setExamid(1);//寰楀埌璇曢鐨刬d
            while (trueAnswer[j]!=null){
                if (num!=0){
                    answerCardInfo.setUserAnswer(trueAnswer[j]);//鐢ㄦ埛鐨勭瓟妗�
                    answerCardInfo.setAnswerStatus(index);//鎻掑叆绛旀鐨勭姸鎬� 0 琛ㄧず鍗曢�夐 1琛ㄧず澶氶�夐
                    ub.InsertAnswer(answerCardInfo);
                    num--;   j++;
                }else{
                    num=5;  index++;
                }
            }
        }
        result=JSONArray.toJSONString(map);
//            if (StringUtils.isNotBlank(callback)){
//                // jsonp璇锋眰
//                // 锛屼娇鐢ㄥ瓧绗︿覆鎷兼帴鐨勬柟寮忥紝鐢熸垚涓�娈礿s鑴氭湰鐗囨
        return callback + "(" + result + ");";
//            }
    }

    @RequestMapping("/findAllCompany")
    @ResponseBody
    public String findAllCompany(String callback,HttpServletRequest request){   //寰楀埌鎵�鏈夊叕鍙哥殑鍚嶇О淇℃伅

        List<CompanyInfo> ci=ub.findAllCompany();
        String json = JSONArray.toJSONString(ci);
        // 鍒ゆ柇璇锋眰鏄惁鏄痡osnp璇锋眰
//            if (StringUtils.isNotBlank(callback)){
//                // jsonp璇锋眰
//                // 锛屼娇鐢ㄥ瓧绗︿覆鎷兼帴鐨勬柟寮忥紝鐢熸垚涓�娈礿s鑴氭湰鐗囨
        return callback + "(" + json + ");";
//            }
        //  闈瀓sonp璇锋眰
    }
    @RequestMapping("/score")
    public String findScore(int UsersId,String callback,HttpServletRequest request){     //灞曠ず濂藉淇℃伅  闇�瑕佺殑淇℃伅锛氱敤鎴穒d锛�
        String result=" ";
        CourseInfo ci=new CourseInfo();
        List<UserCourse> userCourses=new ArrayList<>();
        StudygroupnumberInfo sgi=new StudygroupnumberInfo();
        UserCourse userCourse=new UserCourse();  //杩欐槸涓�涓泦鍚堝璞�
        UserInfo userInfo=ub.findAll();//鏌ュ埌鎵�鏈夌殑鐢ㄦ埛鐨勪俊鎭�
        ScoreInfo si=new ScoreInfo();
        //閫氳繃鐢ㄦ埛鐨刬d 鍜屽叕鍙哥殑id 鍘绘煡璇粬 鐨勮绋嬨��
        CourseUserList courseUserList=new CourseUserList();
        List<CourseInfo>  course = new ArrayList<>();//瀛樻斁鐨勬槸鎵�鏈夌殑璇剧▼淇℃伅銆�
        List<CourseUserList> list=ub.findAllUserCourse(UsersId); //杩欐槸涓�涓泦鍚堬紝鐢ㄦ埛鎵�鏈夌殑璇剧▼id
        for (CourseUserList cu:list) {
            ci.setCompanyId(userInfo.getCompanyId());//寰楀埌鍏徃 id
            ci.setCourseId(cu.getCourseId());//鎵�鏈夌殑璇剧▼id  杩涜鏌ヨ
            course=ub.findAllCourse(ci);  //瀛樻斁鐨勬槸鎵�鏈夌殑璇剧▼淇℃伅銆�
        }
        for (CourseInfo ci1:course) {
            //瀛︾粍琛ㄧ殑淇℃伅
            sgi.setUsersId(userInfo.getUsersId());//闇�瑕佺敤鎴稩D
            sgi.setStudyGroupNumber(ci1.getStudyGroupNumber());//闇�瑕佸缁勫彿
            StudygroupnumberInfo sgi1=ub.findStudygroupnumber(sgi);//寰楀埌鎵�鏈夌殑鐢ㄦ埛鐨勫缁勫悕锛屼紶閫掑缁勫悕鍜岀敤鎴稩D
            //鑰冭瘯琛ㄤ腑鐨勪俊鎭�
            si.setUsersId(userInfo.getUsersId()); //闇�瑕佺敤鎴稩D
            si.setExamId(ci1.getExamId()); //璇曢ID
            ScoreInfo si1=ub.findAllScore(si);//寰楀埌鎵�鏈夌殑鎴愮哗淇℃伅
            //寰楀埌鎵�鏈夌殑璇曢琛ㄤ俊鎭�
            ExamInfo examInfo = ub.findAllExam(ci1.getExamId());
            //灏嗕俊鎭斁鍏ist闆嗗悎涓�
            userCourse.setCourseName(ci1.getCourseName()); //璇剧▼鍚嶇О
            userCourse.setCoursePicturePath(ci1.getCoursePicturePath());  //璇剧▼鍥剧墖
            userCourse.setStudyGroupName(sgi1.getStudyGroupName());  //瀛︾粍鍚�
            userCourse.setExamName(examInfo.getExamName());  //娴嬭瘯鍚�
            if (si1.getExaminationScore()>0){
                userCourse.setExaminationScore(si1.getExaminationScore());
            }else if (si1.getExamStatus()==0){
                userCourse.setExamStatus(0);//琛ㄧず绛夊緟鎴愮哗
            }else if (si1.getExamStatus()==-1){
                userCourse.setExamStatus(-1);//琛ㄧず鏈畬鎴�
            }
            userCourses.add(userCourse); //灏嗘墍鏈夌殑淇℃伅鍔犲叆鍒癠serCourse瀵硅薄闆嗗悎涓�
        }
        String json = JSONArray.toJSONString(userCourses);  //杩欐槸涓�涓泦鍚堝璞�
        // 鍒ゆ柇璇锋眰鏄惁鏄痡osnp璇锋眰
//            if (StringUtils.isNotBlank(callback)){
//                // jsonp璇锋眰
//                // 锛屼娇鐢ㄥ瓧绗︿覆鎷兼帴鐨勬柟寮忥紝鐢熸垚涓�娈礿s鑴氭湰鐗囨
        return callback + "(" + json + ");";
//            }

    }

    @RequestMapping("/checkUser")   //鐢ㄦ埛鐨勭櫥褰曚俊鎭獙璇� 瀹屾垚鐧诲綍
    public String findByUser(String callback,Integer usersName, Integer passWd,HttpServletRequest request){
        String result="";
        UserInfo ui1=ub.findAll();  //浠庢暟鎹簱涓緱鍒扮敤鎴蜂俊鎭�
        System.out.println(ui1);
        if (ui1.getUsersName().equals(usersName)&&ui1.getPassWd().equals(passWd)){
            result = JSONArray.toJSONString(0);  //鐢ㄦ埛宸茬粡瀛樺湪浜嗭紝鐩存帴鍙互鐧诲綍浜嗐�� 0 鍙互鐧诲綍浜�
        }else{
            result = JSONArray.toJSONString(1); //杈撳叆鐨勪俊鎭湁璇�  1  琛ㄧず淇℃伅閿欒
        }
        //            if (StringUtils.isNotBlank(callback)){
//                // jsonp璇锋眰
//                // 锛屼娇鐢ㄥ瓧绗︿覆鎷兼帴鐨勬柟寮忥紝鐢熸垚涓�娈礿s鑴氭湰鐗囨
        return callback + "(" + result + ");";
//            }
    }

    @RequestMapping("/checkCompany")  //鐢ㄦ埛杩涜婵�娲�
    public String findByUserCompany(String callback,String usersName,Integer companyId,String passWd,Integer activationCode,HttpServletRequest request){
        String result="";
        CompanyInfo ci1 = ub.findCompanyId(companyId); //浠庡叕鍙镐腑寰楀埌鐨勮鍏徃鐨勫瘑閽�
        //娌℃湁 婵�娲荤殑鐢ㄦ埛瑕佹彃鍏ユ暟鎹簱涓�
        UserInfo ui1=new UserInfo();
        if (activationCode.equals(ci1.getActivationCode())){
            //灏嗘暟鎹彃鍏ユ暟鎹簱
            ui1.setUsersName(usersName);
            ui1.setUsersName(passWd);
            ub.insertUser(ui1);//灏嗕俊鎭彃鍏ユ暟鎹簱涓�  浼犵殑鏄璞°��
            result = JSONArray.toJSONString(0);// 鍙互娉ㄥ唽浜� 璺宠浆鍒扮櫥褰曠晫闈� 0 鎴愬姛浜�
        }else {
            result = JSONArray.toJSONString(1);//楠岃瘉鐮侀敊璇� 1 琛ㄧず澶辫触浜�
        }
        //            if (StringUtils.isNotBlank(callback)){
//                // jsonp璇锋眰
//                // 锛屼娇鐢ㄥ瓧绗︿覆鎷兼帴鐨勬柟寮忥紝鐢熸垚涓�娈礿s鑴氭湰鐗囨
        return callback + "(" + result + ");";
//            }
    }
    
    
    public Integer changePasswd(@RequestParam("usersId") Integer usersId, @RequestParam("passwd") String passwd, @RequestParam("newpassword") String newpassword,String callback) {
        int i;
        if (passwd.equals(ub.findPasswd(usersId))) {
            if (passwd.equals(newpassword)) {
                i = 0;
            } else {
                ub.changePasswd(usersId, newpassword);
                i = 1;
            }
        } else {
            i = 2;
        }
        return PreAddCallback.preAddCallback(JSONArray.toJSONString(i),callback);
    }
    @RequestMapping(value = "/changeavatarpath", produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public void changeAvatarPath(@RequestParam("usersId") Integer usersId, @RequestParam("newavatarPath") String newavatarPath) {
        ub.changeAvatarPath(usersId, newavatarPath);
    }
    public  void setRank (int usersId) {
        List<Integer> studyGroupNumber = ub.findstudyGroupNumber(usersId);
        List<scoreInfo> list = null;
        List<rank> list2 = new ArrayList<>();
        for (Integer integer : studyGroupNumber) {
            List<Integer> courseId = ub.findcourseId(integer);
            for (Integer integer1 : courseId) {
                List<Integer> examId = ub.findexamId(integer1);
                for (Integer integer2 : examId) {
                    list = ub.findmassage(integer2);
                    for (int i = 0; i < list.size() ; i++) {
                        rank r = new rank();
                        r.setExamId(list.get(i).getExamId());
                        r.setUsersId(list.get(i).getUsersId());
                        r.setExamRank(i + 1);
                        list2.add(r);
                    }
                }
            }
        }
        for (rank r1 : list2) {
            ub.setRank(r1);
            System.out.println(r1);
        }

    }

    @RequestMapping("/showscore")
    @ResponseBody
    public String showInfo(int usersId,String callback){
     //   UserController uc=new UserController();
       //uc.setRank(usersId);
        List<Integer> studyGroupNumber = ub.findstudyGroupNumber(usersId);

        List<showRank> list1 = new ArrayList<>();
        List<showRank> list3 = new ArrayList<>();
       // System.out.println(studyGroupId);
        List<showRank> list=new ArrayList<>();
        for (Integer integer : studyGroupNumber) {
            System.out.println(integer);
            showRank s=new showRank();
            studyGroupNumber stn=new studyGroupNumber();
            stn.setStudyGroupId(integer);
            stn.setUsersId(usersId);
            List<String> studyName= ub.findstudyName(stn);
     //       System.out.println(studyName);
            for (String s1 : studyName) {
               s.setStudyGroupName(s1);
                System.out.println(s1);
               list.add(s);
   //             System.out.println("list"+list1);
               }
            List<Integer> courseId=ub.findcourseId(integer);
            for (Integer integer1 : courseId) {
                List<Integer> examId=ub.findexamId(integer1);
                for (Integer integer2 : examId) {
                    List<String> examName=ub.findexamName(integer2);

                    for (int i = 0; i < list.size(); i++) {
                        for (int j = 0; j < examName.size(); j++) {
                            if(i==j) {
                                showRank ss = new showRank();
                                ss.setStudyGroupName(list.get(i).getStudyGroupName());
                                ss.setExamName(examName.get(j));
                             //   list.remove(s);
                                System.out.println("ioo;"+ss);
                                list1.add(ss);
                            }else{
                                continue;
                            }
                            System.out.println(list1.size());
                            System.out.println(list1);
                        }
                    }
       //             System.out.println("list1"+list1);
                }
            }
        }
     //   System.out.println("list1"+list1);
        List<scoreInfo> list2=ub.findScore(usersId);
        for (int i = 0; i < list1.size(); i++) {
            for (int j = 0; j <list2.size() ; j++) {
                if(i==j) {
                    showRank sss = new showRank();
                    sss.setStudyGroupName(list1.get(i).getStudyGroupName());
                    sss.setExamName(list1.get(i).getExamName());
         //           System.out.println(list1.get(j).getExamName());
                    sss.setExaminationScore(list2.get(j).getExaminationScore());
                    sss.setExamRank(list2.get(j).getExamRank());
                    list3.add(sss);
                }else {
                    continue;
                }
            }

        }
        //list.remove(ss);
        PreAddCallBack pp = new PreAddCallBack();
        System.out.println("list3"+list3);
        return  PreAddCallback.preAddCallback(JSONArray.toJSONString(list3),callback);

    }

}
